package com.aplink.generic.sharepreference;

import android.content.Context;
import android.preference.PreferenceManager;

import com.aplink.generic.constant.type.SharePreferenceType;
import com.aplink.generic.controller.GenericApplication;
import com.aplink.generic.pojo.MasterConfig;

public class GenericSharePreference {
    private static final String PREF_APP_VERSION = "APP_VERSION";
    private static final String PREF_AUTO_SYNC_PENDING = "PREF_AUTO_SYNC_PENDING";
    private static final String PREF_CLOUD_MESSAGE = "PREF_CLOUD_MESSAGE";
    private static final String PREF_DATABASE_MODIFIED = "PREF_DATABASE_MODIFIED";
    private static final String PREF_ENABLE_AUTO_SYNC = "PREF_ENABLE_AUTO_SYNC";
    private static final String PREF_FIRST_START = "PREF_FIRST_START";
    private static final String PREF_LOGINED = "PREF_LOGINED";
    private static final String PREF_OFFLINE_MODE = "PREF_OFFLINE_MODE";
    private static final String PREF_REGISTRATION_ID = "PREF_REGISTRATION_ID";
    private static final String PREF_SETUP_ACCOUNT_COMPLETE = "PREF_SETUP_ACCOUNT_COMPLETE";
    private static final String PREF_START_SYNC = "PREF_START_SYNC";
    private static final String PREF_TOKEN = "PREF_TOKEN";
    private static final String PREF_USER_LOGIN = "USER_LOGIN";

    private static GenericSharePreference sInstance = null;

    public static GenericSharePreference getInstance() {
        if (GenericSharePreference.sInstance == null) {
            synchronized (GenericSharePreference.class) {
                if (GenericApplication.getInstance() != null) {
                    GenericSharePreference.sInstance = new GenericSharePreference();
                    GenericSharePreference.sInstance.mContext = GenericApplication
                            .getInstance().getApplicationContext();
                }
            }
        }
        return GenericSharePreference.sInstance;
    }

    public static GenericSharePreference getInstance(final Context context) {
        if (GenericSharePreference.sInstance == null) {
            synchronized (GenericSharePreference.class) {
                if (context != null) {
                    GenericSharePreference.sInstance = new GenericSharePreference();
                    GenericSharePreference.sInstance.mContext = context;
                }
            }
        }
        return GenericSharePreference.sInstance;
    }

    private Context mContext;

    public boolean clearSharePreference() {
        final String registrationID = getRegistrationId();
        final boolean result = PreferenceManager
                .getDefaultSharedPreferences(this.mContext).edit().clear()
                .commit();
        setRegistrationId(registrationID);
        final MasterConfig mMasterConfig = GenericApplication.getInstance()
                .getMasterConfig();
        setOfflineMode(mMasterConfig.isHasOfflineMode());
        if (mMasterConfig.getAutoSync() != null) {
            setEnableAutoSync(mMasterConfig.getAutoSync().isEnable());
        }
        if (mMasterConfig.getCloudNotification() != null) {
            setHasCloudMessage(true);
        }
        return result;
    }

    public int getAppVersion() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getInt(GenericSharePreference.PREF_APP_VERSION, -1);
    }

    public String getRegistrationId() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getString(GenericSharePreference.PREF_REGISTRATION_ID, null);
    }

    public String getToken() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getString(GenericSharePreference.PREF_TOKEN, null);
    }

    public String getUserlogin() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getString(GenericSharePreference.PREF_USER_LOGIN, null);
    }

    public String getValue(final SharePreferenceType sharePreferenceType) {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getString(sharePreferenceType.getValue(), null);
    }

    public boolean hasCloudMessage() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getBoolean(GenericSharePreference.PREF_CLOUD_MESSAGE, false);
    }

    public boolean isAutoSyncPending() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getBoolean(GenericSharePreference.PREF_AUTO_SYNC_PENDING,
                        false);
    }

    public boolean isDatabaseModified() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getBoolean(GenericSharePreference.PREF_DATABASE_MODIFIED,
                        false);
    }

    public boolean isEnableAutoSync() {
        return PreferenceManager
                .getDefaultSharedPreferences(this.mContext)
                .getBoolean(GenericSharePreference.PREF_ENABLE_AUTO_SYNC, false);
    }

    public boolean isFirstStart() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getBoolean(GenericSharePreference.PREF_FIRST_START, true);
    }

    public boolean isLogined() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getBoolean(GenericSharePreference.PREF_LOGINED, false);
    }

    public boolean isOfflineMode() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getBoolean(GenericSharePreference.PREF_OFFLINE_MODE, false);
    }

    public boolean isSetUpAccountComplete() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getBoolean(GenericSharePreference.PREF_SETUP_ACCOUNT_COMPLETE,
                        false);
    }

    public boolean isStartAutoSync() {
        return PreferenceManager.getDefaultSharedPreferences(this.mContext)
                .getBoolean(GenericSharePreference.PREF_START_SYNC, false);
    }

    public GenericSharePreference setAppVersion(final int appVersion) {
        PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
                .putInt(GenericSharePreference.PREF_APP_VERSION, appVersion)
                .commit();
        return this;
    }

    public GenericSharePreference setAutoSyncPending(final boolean result) {
        PreferenceManager
                .getDefaultSharedPreferences(this.mContext)
                .edit()
                .putBoolean(GenericSharePreference.PREF_AUTO_SYNC_PENDING,
                        result).commit();
        return this;
    }

    public GenericSharePreference setDatabaseModified(final boolean result) {
        PreferenceManager
                .getDefaultSharedPreferences(this.mContext)
                .edit()
                .putBoolean(GenericSharePreference.PREF_DATABASE_MODIFIED,
                        result).commit();
        return this;
    }

    public GenericSharePreference setEnableAutoSync(final boolean result) {
        PreferenceManager
                .getDefaultSharedPreferences(this.mContext)
                .edit()
                .putBoolean(GenericSharePreference.PREF_ENABLE_AUTO_SYNC,
                        result).commit();
        return this;
    }

    public GenericSharePreference setFirstStart(final boolean result) {
        PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
                .putBoolean(GenericSharePreference.PREF_FIRST_START, result)
                .commit();
        return this;
    }

    public GenericSharePreference setHasCloudMessage(final boolean result) {
        PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
                .putBoolean(GenericSharePreference.PREF_CLOUD_MESSAGE, result)
                .commit();
        return this;
    }

    public GenericSharePreference setLogined(final boolean result) {
        PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
                .putBoolean(GenericSharePreference.PREF_LOGINED, result)
                .commit();
        return this;
    }

    public GenericSharePreference setOfflineMode(final boolean result) {
        PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
                .putBoolean(GenericSharePreference.PREF_OFFLINE_MODE, result)
                .commit();
        return this;
    }

    public GenericSharePreference setRegistrationId(final String registrationId) {
        PreferenceManager
                .getDefaultSharedPreferences(this.mContext)
                .edit()
                .putString(GenericSharePreference.PREF_REGISTRATION_ID,
                        registrationId).commit();
        return this;
    }

    public GenericSharePreference setStartAutoSync(final boolean result) {
        PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
                .putBoolean(GenericSharePreference.PREF_START_SYNC, result)
                .commit();
        return this;
    }

    public GenericSharePreference setToken(final String registrationId) {
        PreferenceManager.getDefaultSharedPreferences(this.mContext).edit()
                .putString(GenericSharePreference.PREF_TOKEN, registrationId)
                .commit();
        return this;
    }

    public GenericSharePreference setUpAccountComplete(final boolean result) {
        PreferenceManager
                .getDefaultSharedPreferences(this.mContext)
                .edit()
                .putBoolean(GenericSharePreference.PREF_SETUP_ACCOUNT_COMPLETE,
                        result).commit();
        return this;
    }

    public GenericSharePreference setUserlogin(final String registrationId) {
        PreferenceManager
                .getDefaultSharedPreferences(this.mContext)
                .edit()
                .putString(GenericSharePreference.PREF_USER_LOGIN,
                        registrationId).commit();
        return this;
    }
}
